package LoadFromDatabase;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Envelope;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LinearRing;

public class LoadFromDatabase {
	public LoadFromDatabase(){
		
	}
	
	private static GeometryFactory geomfact = new GeometryFactory();
	
	public void loadAndSave(){
		Connection connection = null;

        String dbhost="geo.informatik.fh-nuernberg.de";
        int dbport=5432;
        String dbuser="dbuser";
        String dbpasswd="dbuser";
        String dbname="deproDBMittelfranken";
        
        Statement statement;

        try{
            /* Zugang zur Datenbank einrichten */
            String connectionString="jdbc:postgresql://"+dbhost+":"+dbport+"/"+dbname;
            connection=DriverManager.getConnection(connectionString,dbuser,dbpasswd);
            connection.setAutoCommit(false);  //Getting results based on a cursor
        }catch(Exception e){
            System.out.println("Error initialising DB access: "+e.toString());
            e.printStackTrace();
            System.exit(1);
        }

        try{
            double links = -11.0611;
            double rechts = -11.1039;
            double oben = 49.4717;
            double unten = 49.4246;
            Coordinate[] coords = getCoordinates(links, rechts, oben, unten);
            Geometry square = geomfact.createPolygon(geomfact.createLinearRing(coords),new LinearRing[0]);
            Envelope boundingBox=square.getEnvelopeInternal(); // Bounding Box berechnen
            statement=connection.createStatement();
            statement.setFetchSize(1000);

            Statement streetStatement = statement;
            LoadStreetsFromDB readStreets = new LoadStreetsFromDB(streetStatement, boundingBox, square);
            readStreets.setName("Read Street Thread");
            readStreets.start();

            Statement linkStatement = statement;
            String linkLocation = "long_from>= "+rechts+" AND long_from<= "+links+" AND lat_from>= "+unten+" AND lat_from<= "+oben;
            LoadLinksFromDB readLinks = new LoadLinksFromDB(linkStatement, linkLocation);
            readLinks.setName("Read Links Thread");
            readLinks.start();

            Statement crossingStatement = statement;
            String crossingLocation = "long>="+rechts+" AND long<="+links+" AND lat>="+unten+" AND lat<="+oben;
            LoadCrossingsFromDB readCrossing = new LoadCrossingsFromDB(crossingStatement, crossingLocation);
            readCrossing.setName("Read Crossings Thread");
            readCrossing.start();

            while(readStreets.isAlive() || readLinks.isAlive() || readCrossing.isAlive()){
                Thread.sleep(200);
            }
       }
        catch(Exception e){
            System.out.println("Error processing DB queries: "+e.toString());
            e.printStackTrace();
            System.exit(1);
        }
	}
	
	private static Coordinate[] getCoordinates(double links, double rechts, double oben, double unten){
        Coordinate[] coords = new Coordinate[5];
        coords[0]=new Coordinate(links,oben);
        coords[1]=new Coordinate(rechts,oben);
        coords[2]=new Coordinate(rechts,unten);
        coords[3]=new Coordinate(links,unten);
        coords[4]=coords[0];
        return coords;
    }
	
}
